package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem>
        implements CheckItemService {
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //条件构造
        QueryWrapper<CheckItem> queryWrapper= new QueryWrapper<>();
        //表示显示所有的未进行逻辑删除的数据
        queryWrapper.eq("is_delete",0);
        Page<CheckItem> page =null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            //查询条件是空
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }else{
            //有条件查询
            queryWrapper.like("name",queryPageBean.getQueryString());
            //分页查询
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }
        //讲数据库查询的分页参数数据封装到PageResult中
       PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return pageResult;
    }

    @Override
    public boolean updateItemById(int id) {
        //逻辑删除
        CheckItem checkItem = new CheckItem();
        //将前端要删除的主键设置到实体类中
        checkItem.setId(id);
        //进行逻辑删除,将值设置为1，说明进行删除
        checkItem.setIs_delete(1);
        System.out.println(checkItem.getIs_delete());
        return updateById(checkItem);
    }

    @Override
    public PageResult findPageById(QueryPageBean queryPageBean) {
        //条件构造
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        //表示所有的进行逻辑删除的数据
        queryWrapper.eq("is_delete",1);
        Page<CheckItem> page =null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            //没有查询条件
          page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        }else{
            queryWrapper.like("name",queryPageBean.getQueryString());
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        }
        //数据库查询的分页参数数据封装到PageResult中
        PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return pageResult;
    }

}
